package com.ibeeking.found.id.api.feign.fallback;


import com.ibeeking.found.id.api.common.dto.SerialNumberDTO;
import com.ibeeking.found.id.api.feign.IdFeignClient;
import com.ibeeking.nematos.utils.id.IdGenerator;
import com.ibeeking.nematos.utils.result.ResponseCodeEnum;
import com.ibeeking.nematos.utils.result.ResponseResult;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 降级工场
 *
 * @Author ibeeking
 * @date 2019/1/18
 */
@Component
public class IdFeignFallbackFactory implements FallbackFactory<IdFeignClient> {
    @Override
    public IdFeignClient create(Throwable throwable) {
        return new IdFeignClient() {
            @Override
            public ResponseResult<List<Object>> selectIds(Integer count) {
                List<Object> result = new ArrayList<>();
                for (int i = 0; i < count; i++) {
                    result.add(IdGenerator.getId());
                }
                return ResponseResult.success(result);
            }

            @Override
            public ResponseResult<SerialNumberDTO> generateSerialNo(String keyCode) {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，生成全局唯一code失败请重试!");
            }

        };
    }
}
